﻿using System.Collections.Generic;
using System.Linq;
using yiCommerce.Controllers;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.Statuses;
using yiCommerce.Models.Products;
using yiCommerce.Models.Statuses;
using yiCommerce.Service.Pictures;

namespace yiCommerce.Mapper
{
    public class StatusMapper
    {
        public static StatusModel ToStatusModel(Status status)
        {
            var m = new StatusModel()
                {
                    IsProduct = status.IsProductStatus,
                    Pics = ToStatusPictureModel(status.StatusPictures),
                    Text = status.Text,
                    ProductStatusInfo = ToProductStatusInfo(status.Product)
                };
            return m;
        }
        public static IList<StatusListItemModel> PrepareStatusListItemModel(IEnumerable<Status> statuses)
        {

            if (statuses == null)
            {
                return new List<StatusListItemModel>();
            }
            else
            {
                return statuses.Select(c =>ToStatusListItemModel(c)).ToList();
            }
        }

        public static StatusListItemModel ToStatusListItemModel(Status status )
        {

            var _pictureService = EngineContext.Current.Resolve<IPictureService>();

            return new StatusListItemModel()
                {
                    Id = status.Id,
                    CreatedAt = status.CreatedAt,
                    Text = KeepLinkOutPut.ToHtmlString(status.Text),
                    CustomerId = status.CustomerId,
                    Deleted = status.Deleted,
                    CustomerLogoUrl =status.Customer.CustomerLogoId.HasValue?   _pictureService.GetPictureUrl(status.Customer.CustomerLogo.PictureId, 80, false): _pictureService.GetDefaultPictureUrl(80),
                    CustomerEmail = status.Customer.Email,
                    CustomerName = status.Customer.Username,
                    StatusPictures = ToStatusPictureModel(status.StatusPictures),
                    IsProductStatus = status.IsProductStatus,
                    ProductStatus = ToProductStatus(status.Product)
                };
        }

        private static ProductStatus ToProductStatus(Product product)
        {
            if (product == null)
            {
                return null;
            }
            else
            {
                return new ProductStatus()
                    {
                        ProductId = product.Id,
                        ProductPrice = product.ProductVariants.First().Price,
                        ProductStock = product.ProductVariants.First().StockQuantity,
                    };
            }
        }

        private static ProductStatusInfo ToProductStatusInfo(Product product)
        {
            if (product == null)
            {
                return null;
            }
            else
            {
                return new ProductStatusInfo()
                {
                    ProductId = product.Id,
                    PvInfos = ToProductVariantInfo(product)
                };
            }
        }

        private static IList<ProductVariantInfo> ToProductVariantInfo(Product product)
        {
            var result = product.ProductVariants.Select(c => new ProductVariantInfo()
                {
                    ProductStock = c.StockQuantity,
                    ProductPrice = c.Price,
                    ProductVariantId = c.Id
                }).ToList();
            return result;
        }


        private static IList<StatusPictureModel> ToStatusPictureModel(ICollection<StatusPicture> statusPictures)
        {
            var _pictureService = EngineContext.Current.Resolve<IPictureService>();

            if (statusPictures == null)
            {
                return null;
            }
            else
            {
                return statusPictures.Select(c => new StatusPictureModel()
                    {
                        PicId = c.PictureId,
                        PicUrl = _pictureService.GetPictureUrl(c.PictureId, 80, false),

                    }).ToList();
            }
        }
    }
}